package ciel.menu;

import org.mt4j.MTApplication;
import org.mt4j.components.visibleComponents.shapes.MTRectangle;
import org.mt4j.components.visibleComponents.widgets.buttons.MTSvgButton;
import org.mt4j.input.inputData.InputCursor;
import org.mt4j.input.inputProcessors.IGestureEventListener;
import org.mt4j.input.inputProcessors.MTGestureEvent;
import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapEvent;
import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapProcessor;
import org.mt4j.util.math.Vector3D;

import processing.core.PApplet;
import ciel.res.CielSettings;
import ciel.scene.CielImageScene;
import ciel.utils.CielMath;

public class CielGlobalMenu extends MTRectangle {
	private CielImageScene scene;
	private PApplet pApplet;

	public CielGlobalMenu(PApplet pApplet, float x, float y, float width, float height) {
		super(pApplet, x, y, width, height);
		this.pApplet = pApplet;
		initMenu(pApplet);
		scene = null;
	} 
	
	
	public void setScene(CielImageScene scene) {
		this.scene = scene;
	}
	
	private void initMenu(PApplet pApplet) {
		
		setName(CielSettings.Childs.GLOBALMENU);
		setNoStroke(true);
		setNoFill(true);
		
		GlobalMenuButton turnOff = new GlobalMenuButton(pApplet, "src/ciel/res/button_turnOff_svg.svg");
		GlobalMenuButton reset = new GlobalMenuButton(pApplet, "src/ciel/res/button_reset_svg.svg");
		GlobalMenuButton upload = new GlobalMenuButton(pApplet, "src/ciel/res/button_upload_svg.svg");
		GlobalMenuButton help = new GlobalMenuButton(pApplet, "src/ciel/res/button_help_svg.svg");
		
		turnOff.addGestureListener(TapProcessor.class, new IGestureEventListener() {
			public boolean processGestureEvent(MTGestureEvent ge) {	
				TapEvent te = (TapEvent)ge;
				if(te.getId() == TapEvent.GESTURE_ENDED) {
					askForGlobalDecisionToShutdown();
					hide();
					return true;
				}
				return false;
			}
		});
		
		reset.addGestureListener(TapProcessor.class, new IGestureEventListener() {
			public boolean processGestureEvent(MTGestureEvent ge) {	
				TapEvent te = (TapEvent)ge;
				if(te.getId() == TapEvent.GESTURE_ENDED) {
					askForGlobalDecisionToReset();
					hide();
					return true;
				}
				return false;
			}
		});
		
		upload.addGestureListener(TapProcessor.class, new IGestureEventListener() {
			public boolean processGestureEvent(MTGestureEvent ge) {	
				TapEvent te = (TapEvent)ge;
				if(te.getId() == TapEvent.GESTURE_ENDED) {
					askForGlobalDecisionToUpload();
					hide();
					return true;
				}
				return false;
			}
		});
		
		help.addGestureListener(TapProcessor.class, new IGestureEventListener() {
			public boolean processGestureEvent(MTGestureEvent ge) {	
				TapEvent te = (TapEvent)ge;
				if(te.getId() == TapEvent.GESTURE_ENDED) {
					scene.addHelper(getCenterPointGlobal());
					hide();
					return true;
				}
				return false;
			}
		});
		
		
		
		float menuWidth = (float) (1.1 * (2*turnOff.getHeightXYRelativeToParent()));
		
		this.setWidthLocal(menuWidth);
		this.setHeightLocal(menuWidth);
		
		this.addChild(turnOff);
		this.addChild(reset);
		this.addChild(upload);
		this.addChild(help);
		
		turnOff.setPositionRelativeToParent(new Vector3D(menuWidth/4, menuWidth/4));
		reset.setPositionRelativeToParent(new Vector3D(3*(menuWidth/4), menuWidth/4));
		upload.setPositionRelativeToParent(new Vector3D(menuWidth/4, 3*(menuWidth/4)));
		help.setPositionRelativeToParent(new Vector3D(3*(menuWidth/4), 3*(menuWidth/4)));
	}
	
	public void show(InputCursor cursor) {
		if(!this.isVisible()) {
			setPositionGlobal(cursor.getStartPosition());	
			float angle = Vector3D.angleBetween(cursor.getStartPosition(), cursor.getPosition());
			angle = MTApplication.degrees(angle);
			rotateZGlobal(getCenterPointGlobal(), MTApplication.degrees(angle));
			setUserData("angle", String.valueOf(angle));
			setVisible(true);
		}
	}
	
	public void hide() {
		setVisible(false);
		float angle = -Float.parseFloat((String) getUserData("angle"));
		rotateZGlobal(getCenterPointGlobal(), angle);
	}
	
	public void askForGlobalDecisionToUpload() {
		scene.exportHTML();
		//scene.getCanvas().addChild(new CielGlobalDecision(scene, pApplet, CielSettings.Dimensions.SCREEN_WIDTH, CielSettings.Dimensions.SCREEN_HEIGHT, CielGlobalDecision.MODE_UPLOAD));
	}
	
	public void askForGlobalDecisionToReset() {
		scene.removeAllImagesFromCanvas();
		//scene.getCanvas().addChild(new CielGlobalDecision(scene, pApplet, CielSettings.Dimensions.SCREEN_WIDTH, CielSettings.Dimensions.SCREEN_HEIGHT, CielGlobalDecision.MODE_RESET));
	}
	
	public void askForGlobalDecisionToShutdown() {
		System.exit(0);
		//scene.getCanvas().addChild(new CielGlobalDecision(scene, pApplet, CielSettings.Dimensions.SCREEN_WIDTH, CielSettings.Dimensions.SCREEN_HEIGHT, CielGlobalDecision.MODE_SHUTDOWN));
	}
	

	private class GlobalMenuButton extends MTSvgButton {

		public GlobalMenuButton(PApplet pa, String fileString) { 
			super(pa, fileString);
			initButton();
		}
		
		private void initButton() {
			float buttonScale = CielMath.getScaleDimension(CielSettings.Scale.globalMenuButton, this.getHeightXYGlobal());
			this.scale(buttonScale, buttonScale, buttonScale, new Vector3D(0,0));
		}
		
	}

}
